package org.swift.commons.client;

import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import static org.apache.axis2.namespace.Constants.URI_SOAP12_ENV;

public class BadHttpResponseFault extends AxisFault {
	
	private final static QName RECEIVER_SOAP_FAULT = new QName(URI_SOAP12_ENV, "Receiver");

	private final int httpResponseCode;
	
	public BadHttpResponseFault(HttpURLConnection conn) throws IOException {
		super(RECEIVER_SOAP_FAULT, conn.getResponseMessage(), conn.getURL().toString(), null, null);
		httpResponseCode = conn.getResponseCode();
	}

	@Override
	public String getMessage() {
		StringBuilder builder = new StringBuilder();
		builder.append("Server returned HTTP response code: ").append(httpResponseCode)
			.append(" for URL: ").append(getFaultNode()).append("; ").append(getReason());
		return builder.toString();
	}

	@Override
	public void printStackTrace() {
		// doesn't make sense to print a trace when the error's on another server
	}

	@Override
	public void printStackTrace(PrintStream ps) {
		// doesn't make sense to print a trace when the error's on another server
	}

	@Override
	public void printStackTrace(PrintWriter pw) {
		// doesn't make sense to print a trace when the error's on another server
	}
}
